package myapp.com.xm.myapplication;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xm.httpapi.Annotation.Error;
import com.xm.httpapi.Annotation.Layout;
import com.xm.httpapi.BaseApiStatus.CommonException;
import com.xm.httpapi.BaseView.BaseActivity;
import com.xm.httpapi.BaseView.BaseNewActivity;

import java.util.ArrayList;
import java.util.HashMap;

import myapp.com.xm.myapplication.Utils.FUCK;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
@Layout(R.layout.activity_main)
public class MainActivity extends BaseNewActivity {

    private Button btnCallPhone, btn_list, btn_list2, btn_view,btn_view2;
    private TextView tv;
    private EditText et;
    private Thead1 thead1;
    private Thead2 thead2;
    private SparseArray<String> aa = new SparseArray<>();
    private HashMap<Integer, String> bb = new HashMap<>();

    @Override
    protected void initData(Bundle savedInstanceState) {
        btnCallPhone = findViewById(R.id.btn_call_phone);
        btn_list = findViewById(R.id.btn_list);
        btn_list2 = findViewById(R.id.btn_list2);
        btn_view = findViewById(R.id.btn_view);
        btn_view2=findViewById(R.id.btn_view2);
        tv = findViewById(R.id.tv);
        et = findViewById(R.id.et);
        thead1 = new Thead1(et);
        thead2 = new Thead2(et);
        aa.put(3, "666");
        bb.put(3, "999");
        //  btnCallPhone.setOnClickListener(v -> aaa());
        btnCallPhone.setOnClickListener(new View.OnClickListener() {

            @Error
            @Override
            public void onClick(View v) {
                // aaa();

            }
        });

//        btn_list.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                intent(List2Activity.class);
//            }
//        });
//
//        btn_list2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                intent(List3Activity.class);
//            }
//        });
//
//        btn_view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                intent(Activity4.class);
//            }
//        });
//
//        btn_view2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                intent(ViewActivity.class);
//            }
//        });

        String openId = null;
        try {
            openId = FUCK.desEncrypt("你好1111");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ;
        Log.e("1111",openId);
        btnCallPhone.setText(openId);
        String desDecrypt = null;
        try {
            desDecrypt = FUCK.desDecrypt(openId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("1111",desDecrypt);
    }

    //   MainActivityPermissionsDispatcher.callPhoneWithCheck(this);
    @Override
    protected String showTitle() {
        setTitleColor(R.color.colorAccent,R.color.colorPrimary);
        return "主页";
    }

    @Override
    protected boolean statusBarIsBlack() {
        return false;
    }

    @Override
    protected boolean showLeftArrow() {
        return false;
    }


    void aaa() {
//        bbb();
//        tv.setText("22222");
        //intent(ListActivity.class);
    }

    void bbb() {
        if (TextUtils.isEmpty(et.getText().toString())) throw new CommonException("字段不能为空");
    }


    @NeedsPermission(Manifest.permission.CALL_PHONE)
    void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "18502189235");
        intent.setData(data);
        startActivity(intent);
    }

//    @OnShowRationale(Manifest.permission.CALL_PHONE)
//    void showWhy(final PermissionRequest permissionRequest) {
//        permissionRequest.proceed();
//    }

//    @OnPermissionDenied(Manifest.permission.CALL_PHONE)
//    void showDenied() {
//        toast("应用权限被拒绝");
//    }

    @OnNeverAskAgain(Manifest.permission.CALL_PHONE)
    void onNeverAskAgain() {
//        new IosAlertDialog(this).builder()
//                .setMsg("应用权限被拒绝,为了不影响您的正常使用，请在 权限 中开启对应权限")
//                .setPositiveButton("授权", v -> {
//                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                    Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
//                    intent.setData(uri);
//                    startActivity(intent);
//                }).setNegativeButton("取消", v -> {
//        }).show();
    }

    /**
     * 权限回调，调用PermissionsDispatcher的回调方法
     * 类名+PermissionsDispatcher哦
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
